using UnityEngine;

namespace Exploder.Demo
{
	public class Rocket : MonoBehaviour
	{
		public delegate void OnHit(Vector3 pos);

		public AudioClip GunShot;

		public AudioClip Explosion;

		public AudioSource Source;

		public ParticleSystem SmokeTrail;

		public ParticleSystem ExplosionSmoke;

		public ParticleSystem ExplosionEffect;

		public GameObject RocketStatic;

		public Light RocketLight;

		public float RocketVelocity = 1f;

		public OnHit HitCallback;

		private Ray direction;

		private bool launch;

		private float launchTimeout;

		private Transform parent;

		private Vector3 shotPos;

		private float targetDistance;

		private void Start()
		{
			parent = base.transform.parent;
			launchTimeout = float.MaxValue;
			SmokeTrail.Stop();
			ExplosionEffect.Stop();
			ExplosionSmoke.Stop();
			ExploderUtils.SetActive(SmokeTrail.gameObject, status: true);
			ExploderUtils.SetActive(ExplosionEffect.gameObject, status: true);
			ExploderUtils.SetActive(ExplosionSmoke.gameObject, status: true);
			ExploderUtils.SetActive(RocketStatic.gameObject, status: false);
		}

		public void OnActivate()
		{
			ExploderUtils.SetActive(RocketStatic.gameObject, status: true);
			if ((bool)parent)
			{
				ExploderUtils.SetVisible(base.gameObject, status: false);
			}
		}

		public void Reset()
		{
			ExploderUtils.SetActive(RocketStatic.gameObject, status: true);
		}

		public void Launch(Ray ray)
		{
			direction = ray;
			Source.PlayOneShot(GunShot);
			launchTimeout = 0.3f;
			launch = false;
			ExploderUtils.SetActive(RocketStatic.gameObject, status: false);
			ExploderUtils.SetVisible(base.gameObject, status: true);
			base.gameObject.transform.parent = parent;
			base.gameObject.transform.localPosition = RocketStatic.gameObject.transform.localPosition;
			base.gameObject.transform.localRotation = RocketStatic.gameObject.transform.localRotation;
			base.gameObject.transform.localScale = RocketStatic.gameObject.transform.localScale;
		}

		private void Update()
		{
			if (launchTimeout < 0f)
			{
				if (!launch)
				{
					launch = true;
					base.transform.parent = null;
					SmokeTrail.Play();
					RocketLight.intensity = 2f;
					direction.origin += direction.direction * 2f;
					if (Physics.Raycast(direction, out RaycastHit hitInfo, float.PositiveInfinity))
					{
						shotPos = base.gameObject.transform.position;
						targetDistance = (base.gameObject.transform.position - hitInfo.point).sqrMagnitude;
					}
					else
					{
						targetDistance = 10000f;
					}
				}
				base.gameObject.transform.position += direction.direction * RocketVelocity * Time.timeScale;
				RocketLight.transform.position = base.gameObject.transform.position;
				if ((shotPos - base.gameObject.transform.position).sqrMagnitude > targetDistance)
				{
					Source.PlayOneShot(Explosion);
					HitCallback(base.gameObject.transform.position);
					launchTimeout = float.MaxValue;
					launch = false;
					SmokeTrail.Stop();
					ExplosionEffect.gameObject.transform.position = base.gameObject.transform.position;
					ExplosionSmoke.gameObject.transform.position = base.gameObject.transform.position;
					ExplosionEffect.Emit(1);
					ExplosionSmoke.Emit(1);
					ExploderUtils.SetVisible(base.gameObject, status: false);
					RocketLight.intensity = 0f;
				}
			}
			launchTimeout -= Time.deltaTime;
			if (UnityEngine.Input.GetKeyDown(KeyCode.H))
			{
				HitCallback(base.gameObject.transform.position);
			}
		}
	}
}
